global nu = 3
global plist = "5 10 20"
global medmat = "(med5, med10, med15)"

/**************************
* MODEL FIT 
**************************
insheet using "$dropbox\Rawdata.csv", clear 

foreach var of varlist x_* {
	replace `var' = `var'*1e5
}
foreach var of varlist l_* {
	replace `var' = `var'*1e3
}
gen n = n_i + n_f 
gen l = l_i + l_f 
gen x = x_i + x_f 

gen frac_ni = n_i/(n_i+n_f)
gen frac_li = l_i/(l_i+l_f)
gen frac_xi = x_i/(x_i+x_f)

gen xbar_i= x_i/ n_i
gen xbar_f= x_f/ n_f

rename * *_data
rename nic_data nic

tempfile rawdata 
save `rawdata', replace 
*/

***********************
* MODEL FIT 
***********************
insheet using "$est/ModelFit_1sector.csv", clear 
drop v1
rename v2 nic 
rename v3 n_i_model
rename v4 n_i_data
rename v5 n_f_model
rename v6 n_f_data
rename v7 l_i_model
rename v8 l_i_data
rename v9 l_f_model
rename v10 l_f_data
rename v11 lbar_i_model 
rename v12 lbar_i_data 
rename v13 lbar_f_model 
rename v14 lbar_f_data 
rename v15 ratio_model
rename v16 ratio_data
rename v17 Vl_i_model 
rename v18 Vl_i_data 
rename v19 Vl_f_model 
rename v20 Vl_f_data 
rename v21 coeff_model 
rename v22 coeff_data 


*Generating the variables to compare to the data 
*gen li = lbar_i * n_i 
*gen lf = lbar_f * n_f 
foreach sim in model data {
	gen n_`sim' = n_i_`sim' + n_f_`sim' 
	gen l_`sim' = l_i_`sim' + l_f_`sim' 

	gen lbar2_i_`sim' = l_i_`sim'/n_i_`sim' 
	gen lbar2_f_`sim' = l_f_`sim'/n_f_`sim'

	gen frac_ni_`sim'= n_i_`sim'/(n_i_`sim'+n_f_`sim')
	gen frac_li_`sim'= l_i_`sim'/(l_i_`sim'+l_f_`sim')

*merge 1:1 nic using `rawdata'
*drop _merge 

*fraction allocated to each industry
*	foreach type in l n  {
*		egen temp = max(sum(`type'_i_`sim'+`type'_f_`sim'))
*		gen frac`type'tot_`sim'= (`type'_i_`sim'+`type'_f_`sim')/temp
*		drop temp*
*	}
}


foreach var of varlist Vl* {
	replace `var' = `var'^0.5
}

*Removing outliers 
*foreach var of varlist lbar_f* lbar2_f* {
*	qui summ `var', de 
*	gen sample`var' = `var'<=r(p90)
*	replace `var'= r(p90) if `var'>r(p90)
*}
*replace lbar2_f_model = 200 if lbar2_f_model>200
 
 
*Outsheeting the differences 
global varlist = "n_i n_f lbar_i lbar_f ratio Vl_i Vl_f frac_li frac_ni"
replace n_i_model = n_i_model/1e3 
replace n_f_model = n_f_model/1e3 
replace n_i_data = n_i_data/1e3 
replace n_f_data = n_f_data/1e3 
 

rename *_model model*
rename *_data data*

reshape long model data, i(nic) j(type) string


gen srno = 1 if type == "n_i"
replace srno = 2 if type == "n_f"
replace srno = 3 if type == "lbar_i"
replace srno = 4 if type == "lbar_f"
replace srno = 5 if type == "Vl_i"
replace srno = 6 if type == "Vl_f"
replace srno = 7 if type == "coeff"
replace srno = 8 if type == "frac_li"
replace srno = 9 if type == "frac_ni"
sort srno 
drop if missing(srno)

replace type = "No. of informal firms ('000)" if type == "n_i"
replace type = "No. of formal firms ('000)"  if type == "n_f"
replace type = "Frac. Informal labor"  if type == "frac_li" 
replace type = "Frac. Informal firms"   if type == "frac_ni" 
replace type = "Firm size in informal firms"   if type == "lbar_i" 
 replace type = "Firm size in formal firms"   if type == "lbar_f" 
 replace type = "Variance in log-firm size (Informal)"   if type == "Vl_i"
replace type = "Variance in log-firm size (Formal)"   if type == "Vl_f"
replace type = "Coefficient"   if type == "coeff"

gen target = "Targeted" if srno<=7 
replace target = "Untargeted" if srno>7 
order target, after(type)
drop nic srno


mkmat model data, mat(T)
frmttable using "${output}/calibration/TableA17.tex", statmat(T) sdec(2, 2) ///
	ctitle(" " "Model", "Data") ///
	rtitle("No. of informal firms ('000), Targeted"\"No. of formal firms ('000), Targeted"\"Firm size in informal firms, Targeted"\"Firm size in formal firms, Targeted"\"Variance in log-firm size (Informal), Targeted"\"Variance in log-firm size (Formal), Targeted"\"Coefficient, Targeted"\"Frac. Informal labor, Untargeted"\"Frac. Informal firms, Untargeted") ///
	replace tex pretext(" ") fragment





